﻿(function (JFocus) {
    JFocus.type.Label = function (name) {
        JFocus.type.DisplayObject.apply(this, [name]);

        /**
        * 重写重绘函数
        **/
        this._redraw = function () {
            if (this._hasRedraw && this._handle != null) {
                if (!this._rotationFlag) {
                    this._handle.style.left = this._x + "px";
                    this._handle.style.top = this._y + "px";
                    this._handle.style.width = this._width + "px";
                    this._handle.style.height = this._height + "px";
                } else {
                    this._rotationFlag = false;
                    if (JFocus.isIE()) {
                        //IE浏览器使用滤镜进行旋转
                        var rad = this._rotation * (Math.PI / 180);
                        var cosValue = Math.cos(rad);
                        var sinValue = Math.sin(rad);
                        //原始中心点
                        var srcCX = this._width / 2;
                        var srcCY = this._height / 2;

                        this._handle.style.filter = "progid:DXImageTransform.Microsoft.Matrix(sizingMethod='auto expand', M11=" + cosValue + ", M12=" + (-sinValue) + ", M21=" + sinValue + ", M22=" + cosValue + ")";

                        //转换后中心点
                        rect = this._handle.getBoundingClientRect();
                        var cx = (rect.right - rect.left) / 2;
                        var cy = (rect.bottom - rect.top) / 2;

                        this._handle.style.left = (this._x + srcCX - cx) + "px";
                        this._handle.style.top = (this._y + srcCY - cy) + "px";
                        this._handle.style.width = this._width + "px";
                        this._handle.style.height = this._height + "px";
                    } else {
                        this._handle.style.left = this._x + "px";
                        this._handle.style.top = this._y + "px";
                        this._handle.style.width = this._width + "px";
                        this._handle.style.height = this._height + "px";

                        this._handle.style.OTransform = " rotate(" + this._rotation + "deg)"; 	//Opera
                        this._handle.style.WebkitTransform = " rotate(" + this._rotation + "deg)"; //Webkit核心
                        this._handle.style.MozTransform = " rotate(" + this._rotation + "deg)"; 	//火狐
                        this._handle.style.Transform = "rotate(" + this._rotation + "deg)";
                    }
                }

                if (this._alphaFlag) {
                    this._alphaFlag = false;
                    this._handle.style.filter = "Alpha(Opacity=" + this._alpha + ")"; //IE中设置透明
                    this._handle.style.MozOpacity = this._alpha / 100; 			//火狐
                    this._handle.style.opacity = this._alpha / 100; 				//Webkit核心
                }

                this._handle.style.color = this._textColor;

                this._hasRedraw = false;
            }
        };

        /**
        * 文本颜色
        **/
        this._textColor = "#000000";
        this.textColor = function (value) {
            if (arguments.length > 0) {
                if (this._textColor != value) {
                    this._textColor = value;

                    this.invalidate();
                }
            } else {
                return this._textColor;
            }
        };

        /**
        * 文本
        **/
        this.text = function (value) {
            if (arguments.length > 0) {
                this._handle.innerHTML = value;
            } else {
                return this._handle.innerHTML;
            }
        };

        if (this._handle == null || this._handle.tagName != "LABEL") {
            this._handle = document.createElement("label");
            this._handle.id = this._name;
            this._handle.name = this._name;
        }
        this._handle.style.position = "absolute";
        this._handle.style.overflow = "hidden";
    };
    JFocus.inherited(JFocus.type.Label, JFocus.type.DisplayObject);

    /**
    * 模块加载器
    **/
    JFocus.type.ModuleLoader = function (name) {
        JFocus.type.DisplayObject.apply(this, [name]);

        /**
        * 重写重绘函数
        **/
        this._redraw = function () {
            if (this._hasRedraw && this._handle != null) {

                this._handle.style.left = this._x + "px";
                this._handle.style.top = this._y + "px";
                this._handle.style.width = this._width + "px";
                this._handle.style.height = this._height + "px";

                if (this._alphaFlag) {
                    this._alphaFlag = false;
                    this._handle.style.filter = "Alpha(Opacity=" + this._alpha + ")"; //IE中设置透明
                    this._handle.style.MozOpacity = this._alpha / 100; 			//火狐
                    this._handle.style.opacity = this._alpha / 100; 				//Webkit核心
                }

                if (typeof (this._backgroundImage) == "string") {
                    this._handle.style.backgroundImage = "url(" + this._backgroundImage + ")";
                }

                this._hasRedraw = false;
            }
        };

        /**
        * 模块路径
        **/
        this.url = function (value) {
            if (arguments.length > 0) {
                this._handle.src = value;
            } else {
                return this._handle.src;
            }
        };

        /**
        * 背景图片
        **/
        this._backgroundImage = null;
        this.backgroundImage = function (value) {
            if (arguments.length > 0) {
                if (value != null && this._backgroundImage != value) {
                    this._backgroundImage = value;

                    this.invalidate();
                }
            } else {
                return this._backgroundImage;
            }
        };

        if (this._handle == null || this._handle.tagName != "IFRAME") {
            this._handle = document.createElement("iframe");
            this._handle.id = this._name;
            this._handle.name = this._name;
        }
        this._handle.style.position = "absolute";
        this._handle.style.overflow = "hidden";
        this._handle.frameBorder = 0;
    };
    JFocus.inherited(JFocus.type.ModuleLoader, JFocus.type.DisplayObject);

    /**
    * 简单文本输入框
    **/
    JFocus.type.SimpleTextFiled = function (name) {
        JFocus.type.DisplayObject.apply(this, [name]);

        var self = this;

        /**
        * @inheritDoc
        **/
        this._redraw = function () {
            if (this._hasRedraw && this._handle != null) {

                this._handle.style.left = this._x + "px";
                this._handle.style.top = this._y + "px";
                this._handle.style.width = this._width + "px";
                this._handle.style.height = this._height + "px";

                this._handle.style.background = this._background;
                this._handle.style.border = this._border;

                if (this._text == null || this._text == "") {
                    if (this._placeholder != null) {
                        this._handle.value = this._placeholder;
                        this._handle.style.color = this._placeholderTextColor;
                    }
                } else {
                    this._handle.value = this._text;
                    this._handle.style.color = this._textColor;
                }

                this._hasRedraw = false;
            }
        };

        /**
        * 文本值变更事件处理器
        **/
        this._textChangeHandler = function (event) {
            self.text = this.value;
        };

        /**
        * 取得焦点事件处理器
        **/
        this._focusHandler = function (event) {
            if (self._text == null) {
                self._handle.value = "";
            } else {
                self._handle.value = self._text;
            }

            self._handle.style.color = this._textColor;
        };

        /**
        * 失去焦点事件处理器
        **/
        this._blurHandler = function (event) {
            if (self._text == null || self._text == "") {
                if (self._placeholder != null) {
                    self._handle.value = self._placeholder;
                    self._handle.style.color = self._placeholderTextColor;
                }
            }
        };

        /**
        * 文本内容
        **/
        this._text = null;
        this.text = function (value) {
            if (arguments.length > 0) {
                if (this._text != value) {
                    this._text = value;

                    this.invalidate();
                }
            } else {
                return this._text;
            }
        };

        /**
        * 提示信息
        **/
        this._placeholder = null;
        this.placeholder = function (value) {
            if (arguments.length > 0) {
                if (this._placeholder != value) {
                    this._placeholder = value;

                    this.invalidate();
                }
            } else {
                return this._placeholder;
            }
        };

        /**
        * 文本颜色
        **/
        this._textColor = "#000000";
        this.textColor = function (value) {
            if (arguments.length > 0) {
                if (this._textColor != value) {
                    this._textColor = value;

                    this.invalidate();
                }
            } else {
                return this._textColor;
            }
        };

        /**
        * 提示文本颜色
        **/
        this._placeholderTextColor = "#cccccc";
        this.placeholderTextColor = function (value) {
            if (arguments.length > 0) {
                if (this._placeholderTextColor != value) {
                    this._placeholderTextColor = value;

                    this.invalidate();
                }
            } else {
                return this._placeholderTextColor;
            }
        };

        /**
        * 背景样式
        **/
        this._background = "none";
        this.background = function (value) {
            if (arguments.length > 0) {
                if (this._background != value) {
                    this._background = value;

                    this.invalidate();
                }
            } else {
                return this._background;
            }
        };

        /**
        * 边框样式
        **/
        this._border = "none";
        this.border = function (value) {
            if (arguments.length > 0) {
                if (this._border != value) {
                    this._border = value;
                    this.invalidate();
                }
            } else {
                return this._border;
            }
        };


        if (this._handle == null || this._handle.tagName != "INPUT") {
            this._handle = document.createElement("input");
            this._handle.type = "text";
            this._handle.id = this._name;
            this._handle.name = this._name;
        }
        this._handle.style.position = "absolute";
        this._handle.style.overflow = "hidden";

        this._width = 160;
        this._height = 21;
        this._handle.style.outline = "none";

        //this._handle.onpropertychange = this._textChangeHandler;
        this._handle.addEventListener("input", this._textChangeHandler, false);
        this._handle.onfocus = this._focusHandler;
        this._handle.onblur = this._blurHandler;
    };
    JFocus.inherited(JFocus.type.SimpleTextFiled, JFocus.type.DisplayObject);

    /**
    * 文本输入框
    **/
    JFocus.type.TextField = function (name) {
        JFocus.type.Sprite.apply(this, [name]);

        var LEFT_PADDING = 3;
        var RIGHT_PADDING = 3;
        var TOP_PADDING = 3;
        var BOTTOM_PADDING = 3;

        var LEFT_VIEW_WIDTH = 25;
        var RIGHT_VIEW_WIDTH = 25;

        /**
        对文本框进行排版
        **/
        this._layoutInputHandle = function () {
            var left = LEFT_PADDING;
            var width = this._width - LEFT_PADDING;
            if (this._leftView != null) {
                left += LEFT_VIEW_WIDTH;
                width -= LEFT_VIEW_WIDTH;
            }
            if (this._rightView != null) {
                this._rightView.x = this._width - RIGHT_VIEW_WIDTH;
                width -= RIGHT_VIEW_WIDTH;
            } else {
                width -= RIGHT_PADDING;
            }

            this._inputHandle.x(left);
            this._inputHandle.width(width);
        };

        /**
        * @inheritDoc
        **/
        this.width = function (value) {
            if (arguments.length > 0) {
                if (this._width != value) {
                    this._width = value;
                    this._layoutInputHandle();

                    this.invalidate();
                }
            } else {
                return this._width;
            }
        };

        /**
        * @inheritDoc
        **/
        this.height = function (value) {
            if (arguments.length > 0) {
                if (this._height != value) {
                    this._height = value;

                    if (this._leftView) {
                        this._leftView.height(value);
                    }
                    if (this._rightView) {
                        this._rightView.height(value);
                    }

                    this._inputHandle.y(TOP_PADDING);
                    this._inputHandle.height(value - TOP_PADDING - BOTTOM_PADDING);

                    this.invalidate();
                }
            } else {
                return this._height;
            }
        };

        /**
        * 左边视图
        **/
        this._leftView = null;
        this.leftView = function (value) {
            if (arguments.length > 0) {
                if (value instanceof JFocus.type.DisplayObject) {
                    if (this._leftView != value) {
                        if (this._leftView != null) {
                            if (this.contains(this._leftView)) {
                                this.removeChild(this._leftView);
                            }
                        }

                        this._leftView = value;
                        this._leftView.width(LEFT_VIEW_WIDTH);
                        this._leftView.height(this._height);
                        this._layoutInputHandle();

                        if (!this.contains(this._leftView)) {
                            this.addChild(this._leftView);
                        }
                    }
                }
            } else {
                return this._leftView;
            }
        };

        /**
        * 右视图
        **/
        this._rightView = null;
        this.rightView = function (value) {
            if (arguments.length > 0) {
                if (value instanceof JFocus.type.DisplayObject) {
                    if (this._rightView != value) {
                        if (this._rightView != null) {
                            if (this.contains(this._rightView)) {
                                this.removeChild(this._rightView);
                            }
                        }
                        this._rightView = value;
                        this._rightView.x(this._width - RIGHT_VIEW_WIDTH);
                        this._rightView.width(RIGHT_VIEW_WIDTH);
                        this._rightView.height(this._height);
                        this._layoutInputHandle();

                        if (!this.contains(this._rightView)) {
                            this.addChild(this._rightView);
                        }
                    }
                }
            } else {
                return this._rightView;
            }
        };

        /**
        * 文本内容
        **/
        this.text = function (value) {
            if (arguments.length > 0) {
                this._inputHandle.text(value);
            } else {
                return this._inputHandle.text();
            }
        };

        /**
        * 提示信息
        **/
        this.placeholder = function (value) {
            if (arguments.length > 0) {
                this._inputHandle.placeholder(value);
            } else {
                return this._inputHandle.placeholder();
            }
        };

        /**
        * 文本颜色
        **/
        this.textColor = function (value) {
            if (arguments.length > 0) {
                this._inputHandle.textColor(value);
            } else {
                return this._inputHandle.textColor();
            }
        };

        /**
        * 提示文本颜色
        **/
        this.placeholderTextColor = function (value) {
            if (arguments.length > 0) {
                this._inputHandle.placeholderTextColor(value);
            } else {
                return this._inputHandle.placeholderTextColor();
            }
        };

        this._backgroundColor = "#ffffff";
        this._width = 160;
        this._height = 23;
        this._inputHandle = new JFocus.type.SimpleTextField(name + "_textHandle");
        this.addChild(this._inputHandle);
    };
    JFocus.inherited(JFocus.type.TextField, JFocus.type.Sprite);

    /**
    * 创建一个标签控件
    * @return 标签控件
    **/
    JFocus.label = function (name) {
        return new JFocus.type.Label(name);
    };

    /**
    * 创建一个模块加载器控件
    * @return 模块加载器控件
    **/
    JFocus.moduleLoader = function (name) {
        return new JFocus.type.ModuleLoader(name);
    };

    /**
    * 创建一个简单文本输入框控件
    * @return 简单文本输入框控件
    **/
    JFocus.simpleTextField = function (name) {
        return new JFocus.type.SimpleTextFiled(name);
    };

    /**
    * 创建一个文本输入框控件
    * @return 文本输入框控件
    **/
    JFocus.textField = function (name) {
        return new JFocus.type.TextField(name);
    };
})(window.JFocus);